package get

import (
	"database/sql"
	"fmt"

	"github.com/gin-gonic/gin"
)

func Updatestudent(r *gin.Engine) {
	var db *sql.DB
	var err error
	var sid, name, gender, sdept, class, college string = "0", "0", "0", "0", "0", "0"

	psqlInfo := fmt.Sprintf("host=%s port=%d user=%s "+
		"password=%s dbname=%s sslmode=disable",
		host, port, user, password, dbname)
	db, err = sql.Open("postgres", psqlInfo)
	if err != nil {
		panic(err)
	}

	//修改数据
	r.GET("/updatestudent", func(c *gin.Context) {
		sid = c.Query("sid")
		name = c.Query("name")
		sdept = c.Query("sdept")
		class = c.Query("class1")
		college = c.Query("college")
		gender = c.Query("gender")

		str := "update student set name=$1,sdept=$2,class=$3,college=$4,gender=$5 where sid=$6"
		stmt, err := db.Prepare(str)
		if err != nil {
			panic(err)
		}

		res, err := stmt.Exec(name, sdept, class, college, gender, sid)
		if err != nil {
			panic(err)
		}

		affect, err := res.RowsAffected()
		if err != nil {
			panic(err)
		}

		fmt.Println("rows affect:", affect)
	})

	//删除用户
	r.GET("/deletestudent", func(c *gin.Context) {
		sid = c.Query("sid")

		if err != nil {
			panic(err)
		}

		stmt, err := db.Prepare("delete from student where sid=$1")
		if err != nil {
			panic(err)
		}

		res, err := stmt.Exec(sid)
		if err != nil {
			panic(err)
		}

		affect, err := res.RowsAffected()
		if err != nil {
			panic(err)
		}

		fmt.Println(" Delete : rows affect:", affect)
	})
}
